# -*- coding:utf8 -*- #
# -----------------------------------------------------------------------------------
# ProjectName:   smart-water-iot-test
# FileName:     get_path
# Author:
# Datetime:    2022/4/11 15:17
# Description：
# -----------------------------------------------------------------------------------
import configparser
import os
import sys


class ConfigUtil:
    def __init__(self):
        # 实例化对象
        self.config = configparser.ConfigParser()
        # 获取项目文件路径后通过common进行切分,选择第一段self.beforePath = ""
        if getattr(sys, 'frozen', False):
            self.beforePath = os.path.dirname(os.path.realpath(sys.executable)).split("dist")[0]
        elif __file__:
            self.beforePath = os.path.dirname(os.path.abspath(__file__)).split("common")[0]
        # 找到inin文件路径
        self.config.read(fr"{self.beforePath}conf/configIni/config.ini", encoding="utf-8-sig")

    def get_connackData_path(self) -> str:
        # 获取ini文件配置的文件
        filePath = self.config.get("FilePath", "connackDataPath")
        # 进行文件路径拼接
        return os.path.join(self.beforePath, filePath)

    def get_sql_json_path(self) -> str:
        filePath = self.config.get("FilePath", "sqlJsonPath")
        return os.path.join(self.beforePath, filePath)

    def get_database_status(self) -> bool:
        """
            ;True：线上环境
            ;False：测试环境
        """
        status = self.config.get("Database_status", "dataBaseEnvironment")
        if status.lower() == "true":
            return True
        else:
            return False

    def get_log_path(self, logType: int) -> str:
        """
        logType=1,mqttlog,logType=2,chargelog;
        True：线上环境,False：测试环境
        """
        dataBaseStatus = self.get_database_status()
        if logType == 1 and dataBaseStatus is True:
            filePath = self.config.get("FilePath", "mqttLogPro")
            return os.path.join(self.beforePath, filePath)
        elif logType == 1 and dataBaseStatus is False:
            filePath = self.config.get("FilePath", "mqttLogTest")
            return os.path.join(self.beforePath, filePath)
        elif logType == 2 and dataBaseStatus is True:
            filePath = self.config.get("FilePath", "chargeLogPro")
            return os.path.join(self.beforePath, filePath)
        elif logType == 2 and dataBaseStatus is False:
            filePath = self.config.get("FilePath", "chargeLogTest")
            return os.path.join(self.beforePath, filePath)
        else:
            print("传参错误，暂未找到相关log路径！")

    # 声明方法，读取配置文件中的数据库信息
    def get_database_info(self, option) -> str:
        """

        :param option:
        :param dataBaseType: 1 hvac_db
                             2 zndb
        :return:
        """
        if self.get_database_status() is True:
            return self.config.get("Database_Pro", option)
        else:
            return self.config.get("Database_Test", option)

    def get_mqtt_url(self):
        getDevicePassword = self.config.get("MQTT", "getDevicePassword")
        if self.get_database_status() is True:
            mqttUrl = self.config.get("MQTT", "mqtt_pro_url")
        else:
            mqttUrl = self.config.get("MQTT", "mqtt_test_url")
        return mqttUrl + getDevicePassword

    def get_control_command(self, status: bool = True):
        """
        :param status:指令状态:
                        TRUE : 同步
                        FALSE : 异步
        :return:
        """
        commandApi = None
        if self.get_database_status() is True:
            mqttUrl = self.config.get("MQTT", "mqtt_pro_url")
        else:
            mqttUrl = self.config.get("MQTT", "mqtt_test_url")
        if status is True:
            commandApi = self.config.get("MQTT", "syncCommand")
        elif status is False:
            commandApi = self.config.get("MQTT", "asyncCommand")
        return mqttUrl + commandApi

    def get_mqtt_config(self, option: str):
        return self.config.get("MQTT", option)

    def get_productkey(self):
        if self.get_database_status() is True:
            return self.config.get("ProductKey", "Pro_productKey")
        else:
            return self.config.get("ProductKey", "Test_productKey")


if __name__ == '__main__':
    # print(ConfigUtil().get_database_info("SSH_Proxy_port",2))
    print(ConfigUtil().get_control_command())
